#include <dalelib/graphics.h>
#include <IL/il.h>
#include <IL/ilut.h>


namespace dalelib
{

void drawQuad(const float _Left, const float _Right, const float _Bottom, const float _Top)
{
	glBegin(GL_QUADS);
		glTexCoord2f( 0.f, 0.f ); glVertex2f( _Left,  _Bottom );
		glTexCoord2f( 1.f, 0.f ); glVertex2f( _Right, _Bottom );
		glTexCoord2f( 1.f, 1.f ); glVertex2f( _Right, _Top    );
		glTexCoord2f( 0.f, 1.f ); glVertex2f( _Left,  _Top    );
	glEnd();
}

void drawGround(const float _Size, const float _TexScale)
{
	float texsize = _Size/_TexScale;
	glBegin(GL_QUADS);
		glNormal3f(0,1,0);
		glTexCoord2f(-texsize,  texsize ); glVertex3f(-_Size, 0.f, _Size );
		glTexCoord2f( texsize,  texsize ); glVertex3f( _Size, 0.f, _Size );
		glTexCoord2f( texsize, -texsize ); glVertex3f( _Size, 0.f,-_Size );
		glTexCoord2f(-texsize, -texsize ); glVertex3f(-_Size, 0.f,-_Size );
	glEnd();
}

void drawTrihedron(const v3 _Scale)
{
	glPushAttrib(GL_CURRENT_BIT|GL_ENABLE_BIT);
	glDisable(GL_LIGHTING);
	glBegin(GL_LINES);
		glColor4f(1,0,0,1); glVertex3f(0,0,0); glVertex3f(_Scale[0],0,0);
		glColor4f(0,1,0,1); glVertex3f(0,0,0); glVertex3f(0,_Scale[1],0);
		glColor4f(0,0,1,1); glVertex3f(0,0,0); glVertex3f(0,0,_Scale[2]);
	glEnd();
	glPopAttrib();
}

void drawCircle(const v2 _Pos,const float _Radius,const int _Spokes)
{
	glBegin(GL_LINE_LOOP);
//	glBegin(GL_LINES);
	for(int i=0; i<_Spokes; ++i)
	{
		float f = TWICEPI*float(i)/_Spokes;
		glVertex2f(_Pos[0]+cosf(f)*_Radius,_Pos[1]+sinf(f)*_Radius);
	}
	glEnd();
}

void drawFilledCircle(const v2 _Pos,const float _Radius,const int _Spokes)
{
	glBegin(GL_TRIANGLE_FAN);
//	glBegin(GL_LINES);
	for(int i=0; i<_Spokes; ++i)
	{
		float f = TWICEPI*float(i)/_Spokes;
		glVertex2f(_Pos[0]+cosf(f)*_Radius,_Pos[1]+sinf(f)*_Radius);
	}
	glEnd();
}

void setFog(const v3 _Colour, const float _Start, const float _End, const float _Density)
{
	GLfloat col[4] = { _Colour.x(), _Colour.y(), _Colour.z(), 1.0f };
	glClearColor( col[0], col[1], col[2], col[3] );
	glFogi(GL_FOG_MODE, GL_LINEAR);
	glFogfv(GL_FOG_COLOR, col);
	glFogf(GL_FOG_DENSITY, _Density);
	glHint(GL_FOG_HINT, GL_NICEST);
	glFogf(GL_FOG_START, _Start);
	glFogf(GL_FOG_END, _End);
	glEnable(GL_FOG);
}

// GLUT_BITMAP_8_BY_13, GLUT_BITMAP_9_BY_15, GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TIMES_ROMAN_24, GLUT_BITMAP_HELVETICA_10, GLUT_BITMAP_HELVETICA_12, GLUT_BITMAP_HELVETICA_18
void printString(v3 _Pos,char *_String,void *_Font)
{
	glRasterPos3fv((float*)_Pos.ptr());
	for(char* c=_String; *c!='\0'; ++c) glutBitmapCharacter(_Font, *c);
}

void saveScreenShot(const string _Path,const string _Suffix,const int _Index)
{
	static int index = 0; // auto incrementing
	char str[256];
	if(_Index>-1) index=_Index; else index++;
	sprintf(str,"%s%.5d.%s",_Path.c_str(),index,_Suffix.c_str());

//	printf("Saving screenshot %s\n",str);

//	GLint viewport[4];
//	glGetIntegerv(GL_VIEWPORT,viewport);
//	int width = viewport[2]-viewport[0];
//	int height = viewport[3]-viewport[1];
//printf("size %d %d\n",viewport[2],viewport[3]);

//	unsigned char *data = new unsigned char[width*height*32); //Allocate memory for storing the image

//	glReadBuffer(GL_BACK);
//	glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
/*
//static bool init = false;
//if(!init)
//{
//	init = true;
	ilInit();
//}
	ILuint il_id;
	ilGenImages( 1, &il_id );
	ilBindImage( il_id );
	ilutGLScreen();
	//ILboolean success = ilLoadImage( _Name.c_str() );

	ilEnable(IL_FILE_OVERWRITE);
	ilSaveImage(str);
	ilDeleteImages(1, &il_id);
//*/
//	delete [] data;

}


} // namespace dalelib
